package orgs.example.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import orgs.example.mapper.UserMapper;
import orgs.example.pojo.User;
import orgs.example.service.UserService;
import orgs.example.utils.Md5Util;
import orgs.example.utils.ThreadLocalUtil;

import java.util.Map;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User findByUserAccount(String userAccount) {
        User user = userMapper.findByUserAccount(userAccount);
        return user;
    }

    @Override
    public void register(User user) {
        userMapper.register(user);
    }

    @Override
    public User getuserinfo() {

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");

        return userMapper.findByUserId(id);
    }

    @Override
    public void edituserInfo(User user) {
        userMapper.edituserInfo(user);
    }

    @Override
    public User findByUserId(Integer userId) {
        return  userMapper.findByUserId(userId);
    }

    @Override
    public void updatePwd(String newPwd) {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("userId");
        User user = new User();
        user.setUserId(id);
//        user.setUserPassword(Md5Util.getMD5String(newPwd));
        user.setUserPassword(newPwd);
        userMapper.edituserInfo(user);
    }
}
